//
//  SKCalendarView.m
//  SKCalendarView
//
//  Created by shevchenko on 17/3/29.
//  Copyright © 2017年 shevchenko. All rights reserved.
//

#import "SKCalendarView.h"
#import "SKConstant.h"
#import "SKCalendarCollectionViewCell.h"
#import "SKWeekCollectionViewCell.h"
#import "SKCalendarManage.h"
#import "WGQTransfersRecordsAlertView.h"
#import "UIView+ShowView.h"//辅助

@interface SKCalendarView () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView * weekCollectionView;
@property (nonatomic, strong) UICollectionView * calendarCollectionView;
@property (nonatomic, strong) SKCalendarManage * calendarManage;
@property (nonatomic, strong) NSDate * theDate;// 当前日期
@property (nonatomic, assign) NSUInteger theYear;// 本年
@property (nonatomic, assign) NSUInteger theDayInMonth;// 今天在本月所处位置
@property (nonatomic, assign) NSInteger selectedRow;// 选择的日期
@property (nonatomic, strong) NSString * displayChineseDate;//已显示的农历日期&节日&节气
@property (nonatomic,strong) NSMutableArray * attendanceArray;//考勤记录的数组
@property (nonatomic,strong) WGQTransfersRecordsBabyModel * babyModel;
@property (nonatomic,copy) NSString * institutions_id;

//请假用到的
@property (nonatomic,copy) NSString * currentDay;

@end

@implementation SKCalendarView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.attendanceArray = [NSMutableArray array];
        self.frame = frame;
        [self customView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(babyChange) name:@"babychange" object:nil];
    }
    return self;
}

- (void)babyChange
{
    self.calendarManage = nil;
}

- (SKCalendarManage *)calendarManage
{
    if (!_calendarManage)
    {
        _calendarManage = [SKCalendarManage manage];
        // 设置初始化日期，默认查看今天所处月份日历
        [_calendarManage checkThisMonthRecordFromToday:[NSDate date]];
        self.theDate = [NSDate date];
        self.theYear = _calendarManage.year;
        [self reloadExternalDate];
    }
    return _calendarManage;
}

- (NSUInteger)lastMonth
{
    if (_lastMonth == 0)
    {
        _lastMonth = self.calendarManage.month - 1;
    }
    return _lastMonth;
}

- (NSUInteger)nextMonth
{
    if (_nextMonth == 0)
    {
        _nextMonth = self.calendarManage.month + 1;
    }
    return _nextMonth;
}

- (NSInteger)selectedRow
{
    if (_selectedRow == 0)
    {
        _selectedRow = - 1;
    }
    return _selectedRow;
}

- (NSInteger)todayInMonth
{
    if (_todayInMonth == 0)
    {
        _todayInMonth = self.calendarManage.todayInMonth;
    }
    return _todayInMonth;
}


#pragma mark - 创建界面
- (void)customView
{
    // 周
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.weekCollectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:layout];
    [self addSubview:self.weekCollectionView];
    self.weekCollectionView.backgroundColor = [UIColor whiteColor];
    self.weekCollectionView.delegate = self;
    self.weekCollectionView.dataSource = self;
    [self.weekCollectionView registerClass:[SKWeekCollectionViewCell class] forCellWithReuseIdentifier:@"Week"];
    [self.weekCollectionView mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.equalTo(self);
        make.left.equalTo(self);
        make.right.equalTo(self);
        
        make.height.mas_offset(self.frame.size.height / 7.5);
        make.height.mas_greaterThanOrEqualTo(40).priorityHigh();
    }];

    // 日期
    UICollectionViewFlowLayout * dateLayout = [[UICollectionViewFlowLayout alloc] init];
    dateLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.calendarCollectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:dateLayout];
    [self addSubview:self.calendarCollectionView];
    self.calendarCollectionView.layer.masksToBounds = YES;
    self.calendarCollectionView.layer.cornerRadius = 7 * kScale;
    self.calendarCollectionView.backgroundColor = [UIColor whiteColor];
    self.calendarCollectionView.delegate = self;
    self.calendarCollectionView.dataSource = self;
    [self.calendarCollectionView registerClass:[SKCalendarCollectionViewCell class] forCellWithReuseIdentifier:@"Calendar"];
    [self.calendarCollectionView mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.equalTo(self.weekCollectionView.mas_bottom);
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.height.mas_offset(6 * (self.frame.size.height / 7.5));
    }];
}

#pragma mark - 外部配置
- (void)setWeekBackgroundColor:(UIColor *)weekBackgroundColor
{
    _weekBackgroundColor = weekBackgroundColor;
    self.weekCollectionView.backgroundColor = weekBackgroundColor;
}

- (void)setNormalInWeekColor:(UIColor *)normalInWeekColor
{
    _normalInWeekColor = normalInWeekColor;
}

- (void)setDayoffInWeekColor:(UIColor *)dayoffInWeekColor
{
    _dayoffInWeekColor = dayoffInWeekColor;
}

- (void)setDateIcon:(UIImage *)dateIcon
{
    _dateIcon = dateIcon;
}

- (void)setHolidayBackgroundColor:(UIColor *)holidayBackgroundColor
{
    _holidayBackgroundColor = holidayBackgroundColor;
}

- (void)setSolarTeromBackgroundColor:(UIColor *)solarTeromBackgroundColor
{
    _solarTeromBackgroundColor = solarTeromBackgroundColor;
}

- (void)setDateBackgroundColor:(UIColor *)dateBackgroundColor
{
    _dateBackgroundColor = dateBackgroundColor;
}

- (void)setSpringColor:(UIColor *)springColor
{
    _springColor = springColor;
}

- (void)setSummerColor:(UIColor *)summerColor
{
    _summerColor = summerColor;
}

- (void)setAutumnColor:(UIColor *)autumnColor
{
    _autumnColor = autumnColor;
}

- (void)setWinterColor:(UIColor *)winterColor
{
    _winterColor = winterColor;
}

- (void)setDateBackgroundIcon:(UIImage *)dateBackgroundIcon
{
    _dateBackgroundIcon = dateBackgroundIcon;
}

- (void)setCalendarTodayTitle:(NSString *)calendarTodayTitle
{
    _calendarTodayTitle = calendarTodayTitle;
}

- (void)setCalendarTodayTitleColor:(UIColor *)calendarTodayTitleColor
{
    _calendarTodayTitleColor = calendarTodayTitleColor;
}

- (void)setHolidayColor:(UIColor *)holidayColor
{
    _holidayColor = holidayColor;
}

- (void)setEnableClickEffect:(BOOL)enableClickEffect
{
    _enableClickEffect = enableClickEffect;
}

- (void)setEnableDateRoundCorner:(BOOL)enableDateRoundCorner
{
    _enableDateRoundCorner = enableDateRoundCorner;
}

- (void)setCheckLastMonth:(BOOL)checkLastMonth
{
    _checkLastMonth = checkLastMonth;
    if (checkLastMonth == YES)
    {
        self.selectedRow = -1;// 重置已选日期
        NSInteger hours = (self.calendarManage.days - 1) * -24;
        NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:self.theDate];
        [self.calendarManage checkThisMonthRecordFromToday:date];
        self.theDate = date;
        [self.calendarCollectionView reloadData];
        [self reloadExternalDate];
    }
}

- (void)setCheckNextMonth:(BOOL)checkNextMonth
{
    _checkNextMonth = checkNextMonth;
    if (checkNextMonth == YES)
    {
        self.selectedRow = -1;// 重置已选日期
        NSUInteger todayInMonth = self.calendarManage.todayInMonth;
        if (todayInMonth > 1) {
            todayInMonth = self.calendarManage.todayInMonth - self.calendarManage.dayInWeek + 2;
        }
        NSUInteger day = self.calendarManage.days - todayInMonth;
        NSInteger hours = (day + 1) * 24;
        NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:self.theDate];
        [self.calendarManage checkThisMonthRecordFromToday:date];
        self.theDate = date;
        [self.calendarCollectionView reloadData];
        [self reloadExternalDate];
    }
}

#pragma mark - 查看指定日期
- (void)checkCalendarWithAppointDate:(NSDate *)date
{
    [self.calendarManage checkThisMonthRecordFromToday:date];
    [self.calendarCollectionView reloadData];
    self.theDate = date;
    [self reloadExternalDate];
}

#pragma mark - 更新外部数据
- (void)reloadExternalDate
{
    self.year = _calendarManage.year;
    self.month = _calendarManage.month;
    self.chineseYear = _calendarManage.chineseYear;
    self.theDayInMonth = _calendarManage.todayPosition;
    self.chineseCalendarMonth = _calendarManage.chineseCalendarMonth;
    self.chineseCalendarDay = _calendarManage.chineseCalendarDay;
    self.chineseCalendarDate = _calendarManage.chineseCalendarDate;
    self.lastMonth = _calendarManage.month - 1;
    self.nextMonth = _calendarManage.month + 1;
}

#pragma mark - 获取节日&节气
- (NSString *)getHolidayAndSolarTermsWithChineseDay:(NSString *)chineseDay
{
    NSString * result = @"";
    NSUInteger row = 0;
    if (self.selectedRow < 0)
    {
        row = self.todayInMonth;// 默认今天
    }
    else
    {
        row = self.selectedRow;
    }
    NSString * date = getNoneNil(self.calendarManage.chineseCalendarDate[row]);
    if (![chineseDay isEqualToString:date])
    {
        result = date;
    }
    return getNoneNil(result);
}


#pragma mark - collectionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.calendarCollectionView)
    {
        if (self.calendarManage.isIncreaseHeight == YES)
        {
            [self.calendarCollectionView mas_updateConstraints:^(MASConstraintMaker *make)
            {
                make.height.mas_offset(6 * (self.frame.size.height / 7.5));
            }];
            return 42;
            
        }
        else
        {
            if (self.calendarCollectionView.frame.size.height > 218)
            {
                [self.calendarCollectionView mas_updateConstraints:^(MASConstraintMaker *make)
                {
                    make.height.mas_offset(5 * (self.frame.size.height / 7.5));
                }];
            }
            return 35;
        }
    }
    
    return self.calendarManage.weekList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 日期
    if (collectionView == self.calendarCollectionView)
    {
        SKCalendarCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Calendar" forIndexPath:indexPath];
        // 是否属于双休日
        if ((indexPath.row + 1) % 7 == 0 || (indexPath.row + 1) % 7 == 1)
        {
            cell.calendarDateColor = FFColor(0x333333);
        }
        else
        {
            cell.calendarDateColor = FFColor(0x333333);
        }
        // 是否属于今天
        if (self.theDayInMonth == indexPath.row && self.calendarManage.month == self.calendarManage.theMonth && self.theYear == self.calendarManage.year)
        {
            cell.calendarDate = getNoneNil(self.calendarManage.calendarDate[indexPath.row]);// 公历日期
            cell.calendarTitle = getNoneNil(self.calendarManage.chineseCalendarDate[indexPath.row]);// 农历日期
            cell.calendarTitle = getNoneNil(self.calendarTodayTitle);
            cell.calendarTitleColor = FFColor(0x999999);
            
        }
        else
        {
            cell.calendarDate = getNoneNil(self.calendarManage.calendarDate[indexPath.row]);// 公历日期
            cell.calendarTitle = getNoneNil(self.calendarManage.chineseCalendarDate[indexPath.row]);// 农历日期
            cell.calendarTitleColor = nil;
        }
        // 对节日&节气进行特殊处理
        if (![self.calendarManage.chineseCalendarDay[indexPath.row] isEqualToString:self.calendarManage.chineseCalendarDate[indexPath.row]])
        {
            NSString * specialDay = self.calendarManage.chineseCalendarDate[indexPath.row];
            // 节气
            if (specialDay.length == 2 && ![specialDay isEqualToString:@"除夕"] && ![specialDay isEqualToString:@"春节"] && ![specialDay isEqualToString:@"七夕"] && ![specialDay isEqualToString:@"元旦"] && ![specialDay isEqualToString:@"小年"])
            {
                cell.dateBackgroundIcon = self.dateBackgroundIcon;
                if (self.calendarManage.month >= 2 && self.calendarManage.month <= 4)
                {
                    // 春季
                    cell.calendarTitleColor = FFColor(0x999999);
                }
                else if (self.calendarManage.month >= 5 && self.calendarManage.month <= 7)
                {
                    // 夏季
                    cell.calendarTitleColor = FFColor(0x999999);
                }
                else if (self.calendarManage.month >= 8 && self.calendarManage.month <= 10)
                {
                    // 秋季
                    cell.calendarTitleColor = FFColor(0x999999);
                }
                else if (self.calendarManage.month >= 11 || self.calendarManage.month == 1)
                {
                    // 冬季
                    cell.calendarTitleColor = FFColor(0x999999);
                }
            }
            else
            {// 节日
                if (specialDay.length == 3 || [specialDay isEqualToString:@"除夕"] || [specialDay isEqualToString:@"春节"] || [specialDay isEqualToString:@"七夕"] || [specialDay isEqualToString:@"元旦"] || [specialDay isEqualToString:@"小年"])
                {
                    cell.calendarTitleColor = FFColor(0x999999);
                    cell.dateBackgroundIcon = self.dateBackgroundIcon;
                }
            }
        }
        else
        {
            cell.calendarTitleColor = FFColor(0x999999);
            cell.dateBackgroundIcon = nil;
        }
        
        // 如果是选中的日期,更改文字颜色和圆形背景颜色
        if (self.selectedRow == indexPath.row && !isEmpty(self.calendarManage.calendarDate[indexPath.row]))
        {
            //开启选中动画
            cell.enableClickEffect = YES;
            //圆形背景色
            cell.dateColor = PROJECT_DEFAULT_COLOR;
            //阳历文字颜色
            cell.calendarDateColor = [UIColor whiteColor];
            //阴历文字颜色
            cell.calendarTitleColor = [UIColor whiteColor];
        }
        else
        {
            //是否有点击效果
            cell.enableClickEffect = NO;
            //圆形背景色
            cell.dateColor = nil;
        }
        
        //展示考勤记录,
        NSString * timeStr = [NSString stringWithFormat:@"%lu-%.2lu-%.2ld",(unsigned long)_calendarManage.year,(unsigned long)_calendarManage.month,(long)[getNoneNil(self.calendarManage.calendarDate[indexPath.row]) integerValue]];
        DLog(@"%@",timeStr);
        [self.attendanceArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
        {
            NSDictionary * dict = obj;
            if ([dict[@"ddate"] isEqualToString:timeStr])
            {
                if ([dict[@"kq_status"] integerValue] == 1)
                {
                    //正常
                    //是否显示圆形背景
                    cell.enableDateRoundCorner = YES;
                    //圆形背景色
                    cell.dateColor = FFColor(0x3dd982);
                    //阳历文字颜色
                    cell.calendarDateColor = [UIColor whiteColor];
                    //阴历文字颜色
                    cell.calendarTitleColor = [UIColor whiteColor];
                }
                if ([dict[@"kq_status"] integerValue] == 2)
                {
                    //增加
                    //是否显示圆形背景
                    cell.enableDateRoundCorner = YES;
                    //圆形背景色
                    cell.dateColor = FFColor(0xff9000);
                    //阳历文字颜色
                    cell.calendarDateColor = [UIColor whiteColor];
                    //阴历文字颜色
                    cell.calendarTitleColor = [UIColor whiteColor];
                }
                if ([dict[@"kq_status"] integerValue] == 3)
                {
                    //请假
                    //是否显示圆形背景
                    cell.enableDateRoundCorner = YES;
                    //圆形背景色
                    cell.dateColor = FFColor(0xf14d4d);
                    //阳历文字颜色
                    cell.calendarDateColor = [UIColor whiteColor];
                    //阴历文字颜色
                    cell.calendarTitleColor = [UIColor whiteColor];
                }
                //找到今天，就停止循环
                *stop = YES;
            }
        }];
        return cell;
    }
    else
    {
        // 周
        SKWeekCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Week" forIndexPath:indexPath];
        cell.week = getNoneNil(self.calendarManage.weekList[indexPath.row]);
        cell.weekBackgroundColor = self.weekCollectionView.backgroundColor;
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.calendarCollectionView])
    {
        self.selectedRow = indexPath.row;
        [self.calendarCollectionView reloadData];
        self.currentDay = getNoneNil(self.calendarManage.calendarDate[indexPath.row]);
        
        //判断点击的日期是否是今天或者之后
        
        //现实中今天的日期
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyyMMdd"];
        NSString * dateTime = [formatter stringFromDate:[NSDate date]];
        
        //点击选中的日期
        NSString * selectDay = [NSString stringWithFormat:@"%lu%.2lu%.2ld",(unsigned long)_calendarManage.year,(unsigned long)_calendarManage.month,(long)[self.currentDay integerValue]];
        
        if ([selectDay integerValue] < [dateTime integerValue])
        {
            //点击的是今天之前
            [MBManager showBriefAlert:@"当前日期不可操作"];
        }
        else
        {
            //今天及以后
            [self creatAlrtView];
        }
    }
}

- (void)creatAlrtView
{
    NSArray * array = @[@"上午请假",@"下午请假",@"全天请假",@"销假"];
    WGQTransfersRecordsAlertView * alertView = [[WGQTransfersRecordsAlertView alloc] initWithFrame:CGRectMake(100 * kScale, (kScreenHeight - array.count * 40 * kScale)/2, 160 * kScale, array.count * 40 * kScale) andTitleArray:array];
    alertView.layer.masksToBounds = YES;
    alertView.layer.cornerRadius = 4 * kScale;
    [alertView show];
    
    //弹框数据的回调
    __weak __typeof(&*self)weakSelf = self;
    alertView.click_index_block = ^(NSInteger index)
    {
        NSString * title;
        if (index <= 2)
        {
            switch (index)
            {
                case 0:
                    {
                        title = @"上午";
                    }
                    break;
                case 1:
                {
                    title = @"下午";
                }
                    break;
                case 2:
                {
                    title = @"上午,下午";
                }
                    break;
                default:
                    break;
            }
            [weakSelf askForLeaveWithTitle:title];
        }
        else
        {
            [weakSelf terminateLeave];
        }
    };
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.frame.size.width / 7.5, self.frame.size.height / 7.5);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark 网络请求
- (void)updataBabayModel:(WGQTransfersRecordsBabyModel *)model institutionsId:(NSString *)institutions_id
{
    self.babyModel = model;
    self.institutions_id = institutions_id;
    [self requestAttendanceRecordsWithBabayModel:model institutionsId:institutions_id];
}

//获取当前宝宝的考勤记录，
//ch_id  宝宝id
//org_id 机构id
//iyear 年度
//imonth 月份
- (void)requestAttendanceRecordsWithBabayModel:(WGQTransfersRecordsBabyModel *)model institutionsId:(NSString *)institutions_id
{
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request)
     {
         request.requestSerializerType = ZBJSONRequestSerializer;
         request.URLString = [NSString stringWithFormat:@"%@%@?ch_id=%@&org_id=%@&iyear=%@&imonth=%@",Baby_attendance,model._id,model._id,institutions_id,[NSString stringWithFormat:@"%lu",(unsigned long)self.calendarManage.year],[NSString stringWithFormat:@"%lu",(unsigned long)self.calendarManage.month]];
         request.methodType = ZBMethodTypeGET;
         [request setValue:userinfo.token forHeaderField:@"token"];
         request.apiType = ZBRequestTypeRefresh;
     } success:^(id responseObject, apiType type)
     {
         [MBManager hideAlert];
         // 解析数据
         NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
         DLog(@"%@",dataDict);
         NSInteger code = [dataDict[@"msgcode"] integerValue];
         if (code == 1)
         {
             // 请求成功,获取考勤数据，刷新当前页面
             [MBManager hideAlert];
             if (self.attendanceArray.count > 0)
             {
                 [self.attendanceArray removeAllObjects];
             }
             NSArray * array = dataDict[@"value"][@"kqlst"];
             [self.attendanceArray addObjectsFromArray:array];
             [self.calendarCollectionView reloadData];
             if (self.data_count_block)
             {
                 NSArray * array = dataDict[@"value"][@"kqtj"];
                 if (array.count > 0)
                 {
                     NSDictionary * dict = dataDict[@"value"][@"kqtj"][0];
                     self.data_count_block(dict);
                 }
             }
         }
         else
         {
             [MBManager showBriefAlert:dataDict[@"message"]];
         }
     } failure:^(NSError *error)
     {
         DLog(@"%@",error);
     } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache)
     {
         if (isCache==YES)
         {
             DLog(@"使用了缓存");
         }
     }];
}

//孩子请假
- (void)askForLeaveWithTitle:(NSString *)title
{
    NSString * timeStr = [NSString stringWithFormat:@"%lu-%.2lu-%.2ld",(unsigned long)_calendarManage.year,(unsigned long)_calendarManage.month,(long)[self.currentDay integerValue]];
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request)
     {
         request.requestSerializerType = ZBJSONRequestSerializer;
         NSString * urlStr = [NSString stringWithFormat:@"%@%@?orgid=%@&ddate=%@&timeblock=%@",Baby_ask_for_leave,self.babyModel._id,self.institutions_id,timeStr,title];
//        NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)urlStr,(CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",NULL,kCFStringEncodingUTF8));
//         request.URLString = encodedString;
         request.URLString = urlStr;
         request.methodType = ZBMethodTypePOST;
         [request setValue:userinfo.token forHeaderField:@"token"];
         request.apiType = ZBRequestTypeRefresh;
     } success:^(id responseObject, apiType type)
     {
         [MBManager hideAlert];
         // 解析数据
         NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
         DLog(@"%@",dataDict);
         NSInteger code = [dataDict[@"msgcode"] integerValue];
         if (code == 1)
         {
             // 请求成功,获取考勤数据，刷新当前页面
             [MBManager hideAlert];
             [self requestAttendanceRecordsWithBabayModel:self.babyModel institutionsId:self.institutions_id];
         }
         else
         {
             [MBManager showBriefAlert:dataDict[@"message"]];
         }
     } failure:^(NSError *error)
     {
         DLog(@"%@",error);
     } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache)
     {
         if (isCache==YES)
         {
             DLog(@"使用了缓存");
         }
     }];
}

//销假
- (void)terminateLeave
{
    NSString * timeStr = [NSString stringWithFormat:@"%lu-%.2lu-%.2ld",(unsigned long)_calendarManage.year,(unsigned long)_calendarManage.month,(long)[self.currentDay integerValue]];
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request)
     {
         request.requestSerializerType = ZBJSONRequestSerializer;
         NSString * urlStr = [NSString stringWithFormat:@"%@%@?orgid=%@&ddate=%@&timeblock=%@",Baby_terminate_leave,self.babyModel._id,self.institutions_id,timeStr,@"上午,下午"];
         request.URLString = urlStr;
         request.methodType = ZBMethodTypePOST;
         [request setValue:userinfo.token forHeaderField:@"token"];
         request.apiType = ZBRequestTypeRefresh;
     } success:^(id responseObject, apiType type)
     {
         [MBManager hideAlert];
         // 解析数据
         NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
         DLog(@"%@",dataDict);
         NSInteger code = [dataDict[@"msgcode"] integerValue];
         if (code == 1)
         {
             // 请求成功,获取考勤数据，刷新当前页面
             [MBManager hideAlert];
             [self requestAttendanceRecordsWithBabayModel:self.babyModel institutionsId:self.institutions_id];
         }
         else
         {
             [MBManager showBriefAlert:dataDict[@"message"]];
         }
     } failure:^(NSError *error)
     {
         DLog(@"%@",error);
     } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache)
     {
         if (isCache==YES)
         {
             DLog(@"使用了缓存");
         }
     }];
}
@end
